﻿using System;
using System.Collections.Generic;
using System.Data;
using DataBase;

namespace DBService
{
	public class DbProjectUserRelation : DbObject
	{
		public DbUser User
		{
			get
			{
				return this.mUser;
			}
			set
			{
				this.mUser = value;
			}
		}

		public DbProject Project
		{
			get
			{
				return this.mProject;
			}
			set
			{
				this.mProject = value;
			}
		}

		public UserType UserType
		{
			get
			{
				return this.mUserType;
			}
			set
			{
				this.mUserType = value;
			}
		}

		public string UserTypeString
		{
			get
			{
				if (this.mUserType == UserType.ArchEngineer)
				{
					return "建筑工程师";
				}
				if (this.mUserType == UserType.MEPEngineer)
				{
					return "水暖电工程师";
				}
				return "结构工程师";
			}
		}

		public bool Valid
		{
			get
			{
				return this.mValid;
			}
			set
			{
				this.mValid = value;
			}
		}

		public DbProjectUserRelation(DbUser user, DbProject project, UserType userType, bool valid)
		{
			this.mUser = user;
			this.mProject = project;
			this.mUserType = userType;
			this.mValid = valid;
		}

		public static List<DbProjectUserRelation> GetByProject(DbProject project)
		{
			List<DbProjectUserRelation> result;
			try
			{
				if (project.Database.Connection.State == ConnectionState.Closed)
				{
					project.Database.Connection.Open();
				}
				List<DbProjectUserRelation> list = new List<DbProjectUserRelation>();
				string sql = string.Format("select UserId,ProjectId,UserType,Valid from tbProjectUserRelation WHERE ProjectId={0}", project.Id);
				DataTable dataTable = project.Database.ExecQuery(sql);
				if (project.Database.Connection.State == ConnectionState.Open)
				{
					project.Database.Connection.Close();
				}
				if (dataTable != null)
				{
					for (int i = 0; i < dataTable.Rows.Count; i++)
					{
						list.Add(new DbProjectUserRelation(DbUser.GetFromDB(Convert.ToInt32(dataTable.Rows[0]["UserId"]), BimServerDB.Default), DbProject.GetFromDB(Convert.ToInt32(dataTable.Rows[0]["ProjectId"]), BimServerDB.Default), (UserType)Convert.ToInt32(dataTable.Rows[0]["UserType"]), Convert.ToBoolean(dataTable.Rows[0]["Valid"])));
					}
				}
				result = list;
			}
			catch (Exception ex)
			{
				if (project.Database.Connection.State == ConnectionState.Open)
				{
					project.Database.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public static List<DbProjectUserRelation> GetInvalidRelation(IDatabase db)
		{
			List<DbProjectUserRelation> result;
			try
			{
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				List<DbProjectUserRelation> list = new List<DbProjectUserRelation>();
				string sql = string.Format("select UserId,ProjectId,UserType,Valid from tbProjectUserRelation where Valid=0", Array.Empty<object>());
				DataTable dataTable = db.ExecQuery(sql);
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				if (dataTable != null)
				{
					for (int i = 0; i < dataTable.Rows.Count; i++)
					{
						try
						{
							DbUser fromDB = DbUser.GetFromDB(Convert.ToInt32(dataTable.Rows[i]["UserId"]), BimServerDB.Default);
							DbProject fromDB2 = DbProject.GetFromDB(Convert.ToInt32(dataTable.Rows[i]["ProjectId"]), BimServerDB.Default);
							UserType userType = (UserType)Convert.ToInt32(dataTable.Rows[i]["UserType"]);
							bool valid = Convert.ToBoolean(dataTable.Rows[i]["Valid"]);
							list.Add(new DbProjectUserRelation(fromDB, fromDB2, userType, valid));
						}
						catch
						{
						}
					}
				}
				result = list;
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public static List<DbProjectUserRelation> GetFromDb(IDatabase db)
		{
			List<DbProjectUserRelation> result;
			try
			{
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				List<DbProjectUserRelation> list = new List<DbProjectUserRelation>();
				string sql = string.Format("select UserId,ProjectId,UserType,Valid from tbProjectUserRelation", Array.Empty<object>());
				DataTable dataTable = db.ExecQuery(sql);
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				if (dataTable != null)
				{
					for (int i = 0; i < dataTable.Rows.Count; i++)
					{
						list.Add(new DbProjectUserRelation(DbUser.GetFromDB(Convert.ToInt32(dataTable.Rows[0]["UserId"]), BimServerDB.Default), DbProject.GetFromDB(Convert.ToInt32(dataTable.Rows[0]["ProjectId"]), BimServerDB.Default), (UserType)Convert.ToInt32(dataTable.Rows[0]["UserType"]), Convert.ToBoolean(dataTable.Rows[0]["Valid"])));
					}
				}
				result = list;
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public override void AddToDb(IDatabase db)
		{
			throw new NotImplementedException();
		}

		public override void UpdateToDb()
		{
			throw new NotImplementedException();
		}

		public override void RemoveFromDb()
		{
			throw new NotImplementedException();
		}

		private DbUser mUser;

		private DbProject mProject;

		private UserType mUserType;

		private bool mValid;
	}
}
